<?php

namespace pay\modules\backend\models;

use Yii;

/**
 * This is the model class for table "pay_order_details".
 *
 * @property int $id
 * @property int $oid 订单id
 * @property string $goods_name 商品名称
 * @property string $goods_desc 商品描述
 * @property int $pay_money 支付金额
 * @property string $is_refund 1-已退款,2-未退款
 * @property string $client_id 客户端ip
 * @property string $facility 客户端设备
 * @property int $refund_money 退款金额
 * @property string $refund_time 退款时间
 * @property string $from 来源
 * @property string $pay_times 支付时间戳，毫秒
 * @property string $callback_url 回调地址
 * @property string $remake 备注
 */
class OrderDetails extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_order_details';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['oid'], 'required'],
            [['oid', 'pay_money', 'refund_money'], 'integer'],
            [['is_refund'], 'string'],
            [['refund_time'], 'safe'],
            [['goods_name', 'goods_desc', 'callback_url'], 'string', 'max' => 255],
            [['client_id'], 'string', 'max' => 32],
            [['facility', 'from'], 'string', 'max' => 64],
            [['pay_times'], 'string', 'max' => 15],
            [['remake'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'oid' => 'Oid',
            'goods_name' => 'Goods Name',
            'goods_desc' => 'Goods Desc',
            'pay_money' => 'Pay Money',
            'is_refund' => 'Is Refund',
            'client_id' => 'Client ID',
            'facility' => 'Facility',
            'refund_money' => 'Refund Money',
            'refund_time' => 'Refund Time',
            'from' => 'From',
            'pay_times' => 'Pay Times',
            'callback_url' => 'Callback Url',
            'remake' => 'Remake',
        ];
    }



    /*** 退款添加信息
     * @param $oid
     * @param $data
     * @return bool
     */
    public function addRefundInfo($oid,$refundFee)
    {
        $model = self::find()->where('oid = :oid',[':oid' => $oid])->one();
        if (!is_null($model)){
            $model->is_refund = '1';
            $model->refund_money = $refundFee;
            $model->refund_time = date("Y-m-d H:i:s");
            if ($model->save(0)){
                return true;
            }
        }
        return false;
    }
}
